POJ-3268 Silver Cow Party (Dijkstra + 逆向建边)

描述

传送门:Silver Cow Party

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

输入描述

Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

输出描述

Line 1: One integer: the maximum of time any one cow must walk.

示例

输入

1
Line 1: One integer: the maximum of time any one cow must walk.

输出

1
2
3
4
5
6
7
8
9
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

题解

题目大意

有向图,首行给出N,M,X代表有N个点,M条边,接着M行每行u,v,w代表u到v权值为w,求出各点到X加上X到各点最小权值中的最大值。

思路

建边的同时,反向建边,各跑一遍Dijkstra。最后遍历所有点维护最大值MAX。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
#include<cmath>
const int MAXN = 1e3 +5, INF = 0x3f3f3f3f;
using namespace std;
int n, m, x;
int a, b, c;
int d[MAXN], dt[MAXN];
vector<pair<int, int> >E[MAXN*MAXN], Et[MAXN*MAXN];

int main(){
cin >> n >> m >> x;
for(int i = 0; i < m; i++){
cin >> a >> b >> c;
E[a].push_back(make_pair(b, c));
Et[b].push_back(make_pair(a, c));
}
memset(d, INF, sizeof(d));
memset(dt, INF, sizeof(dt));
priority_queue<pair<int, int> > que;
d[x] = 0;
que.push(make_pair(-d[x], x));
while(!que.empty()){
int now = que.top().second;
que.pop();
for(int i = 0; i < E[now].size(); i++){
int v = E[now][i].first;
if(d[v] > d[now] + E[now][i].second){
d[v] = d[now] + E[now][i].second;
que.push(make_pair(-d[v], v));
}
}
}
dt[x] = 0;
que.push(make_pair(-d[x], x));
while(!que.empty()){
int now = que.top().second;
que.pop();
for(int i = 0; i < Et[now].size(); i++){
int v = Et[now][i].first;
if(dt[v] > dt[now] + Et[now][i].second){
dt[v] = dt[now]+Et[now][i].second;
que.push(make_pair(-dt[v], v));
}
}
}


int res = 0;
for(int i = 1; i <= n; i++){
if(i == x) continue;
res = max(res, d[i]+dt[i]);
}
cout << res;
}


/*

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

*/